package com.luke.base.service.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.luke.base.bo.DataCount;
import com.luke.base.domain.vo.EquipmentModel;
import com.luke.common.core.domain.entity.SysUser;
import com.luke.common.exception.CustomException;
import com.luke.common.utils.SecurityUtils;
import com.luke.common.utils.StringUtils;
import com.luke.system.service.impl.SysUserServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.luke.base.mapper.LkEquipmentMapper;
import com.luke.base.domain.LkEquipment;
import com.luke.base.service.ILkEquipmentService;

/**
 * 设备管理Service业务层处理
 * 
 * @author luke
 * @date 2021-05-30
 */
@Service
public class LkEquipmentServiceImpl implements ILkEquipmentService 
{
    private static final Logger log = LoggerFactory.getLogger(LkEquipmentServiceImpl.class);
    @Autowired
    private LkEquipmentMapper lkEquipmentMapper;

    /**
     * 查询设备管理
     * 
     * @param id 设备管理ID
     * @return 设备管理
     */
    @Override
    public LkEquipment selectLkEquipmentById(Long id)
    {
        return lkEquipmentMapper.selectLkEquipmentById(id);
    }

    /**
     * 查询设备管理列表
     * 
     * @param lkEquipment 设备管理
     * @return 设备管理
     */
    @Override
    public List<LkEquipment> selectLkEquipmentList(LkEquipment lkEquipment)
    {
        return lkEquipmentMapper.selectLkEquipmentList(lkEquipment);
    }

    /**
     * 新增设备管理
     * 
     * @param lkEquipment 设备管理
     * @return 结果
     */
    @Override
    public int insertLkEquipment(LkEquipment lkEquipment)
    {
        return lkEquipmentMapper.insertLkEquipment(lkEquipment);
    }

    /**
     * 修改设备管理
     * 
     * @param lkEquipment 设备管理
     * @return 结果
     */
    @Override
    public int updateLkEquipment(LkEquipment lkEquipment)
    {
        return lkEquipmentMapper.updateLkEquipment(lkEquipment);
    }

    /**
     * 批量删除设备管理
     * 
     * @param ids 需要删除的设备管理ID
     * @return 结果
     */
    @Override
    public int deleteLkEquipmentByIds(Long[] ids)
    {
        //查询是否绑定用户
        int count=0;
        for (Long id : ids) {
            LkEquipment equipment=lkEquipmentMapper.selectLkEquipmentById(id);
            if(equipment.getUserId()==null){
                lkEquipmentMapper.deleteLkEquipmentById(id);
                count++;
            }
        }
        return count;
    }

    /**
     * 删除设备管理信息
     * 
     * @param id 设备管理ID
     * @return 结果
     */
    @Override
    public int deleteLkEquipmentById(Long id)
    {
        return lkEquipmentMapper.deleteLkEquipmentById(id);
    }

    @Override
    public String importEquipment(List<LkEquipment> equipmentList) {
        if (StringUtils.isNull(equipmentList) || equipmentList.size() == 0)
        {
            throw new CustomException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        //查询原有设备
        List<LkEquipment> eqList=lkEquipmentMapper.selectLkEquipmentList(new LkEquipment());
        //组织成map
        Set<String> equipmentNumberSet =new HashSet();
        Set<String> imeiSet =new HashSet();
        for (LkEquipment equipment : eqList) {
            equipmentNumberSet.add(equipment.getEquipmentNumber());
            imeiSet.add(equipment.getImei());
        }

        for (LkEquipment equipment : equipmentList)
        {
            try
            {
                if(!equipmentNumberSet.contains(equipment.getEquipmentNumber()) && !imeiSet.contains(equipment.getImei())){
                    equipment.setInstallStatus("1");
                    equipment.setActivateStatus("1");
                    this.insertLkEquipment(equipment);
                    equipmentNumberSet.add(equipment.getEquipmentNumber());
                    imeiSet.add(equipment.getImei());
                    successNum++;
                }else {
                    failureNum++;
                }

            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、设备编号 " + equipment.getEquipmentNumber() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }

        /*if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }*/

        successMsg.insert(0, "共导入 "+equipmentList.size()+"条数据！其中成功 " + successNum + " 条，失败 "+failureNum +"条");

        return successMsg.toString();
    }

    @Override
    public List<EquipmentModel> selectLkEquipmentListUninstall() {
        return lkEquipmentMapper.selectLkEquipmentListUninstall();
    }

    @Override
    public String importEquipmentInstall(List<LkEquipment> equipmentList) {
        if (StringUtils.isNull(equipmentList) || equipmentList.size() == 0)
        {
            throw new CustomException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        for (LkEquipment equipment : equipmentList)
        {
            try
            {
                equipment.setInstallStatus("0");
                this.updateLkEquipment(equipment);
                successNum++;
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、设备编号 " + equipment.getEquipmentNumber() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public LkEquipment selectLkEquipmentByNumber(String number) {
        return lkEquipmentMapper.selectLkEquipmentByNumber(number);
    }

    @Override
    public List<LkEquipment> selectLkEquipmentUserList(LkEquipment lkEquipment) {
        return lkEquipmentMapper.selectLkEquipmentUserList(lkEquipment);
    }

    @Override
    public DataCount getSaleCount() {
        return lkEquipmentMapper.getSaleCount();
    }
    @Override
    public DataCount getEquipCount() {
        return lkEquipmentMapper.getEquipCount();
    }

    @Override
    public DataCount getVehicleCount() {
        return lkEquipmentMapper.getVehicleCount();
    }

    @Override
    public List<DataCount> vehicleSaleChart(String beginDate) {
        return lkEquipmentMapper.vehicleSaleChart(beginDate);
    }

    @Override
    public List<DataCount> vehicleSalePie() {
        return lkEquipmentMapper.vehicleSalePie();
    }

    @Override
    public List<DataCount> vehicleSaleBar(String beginDate) {
        return lkEquipmentMapper.vehicleSaleBar(beginDate);
    }
}
